from PyQt5.QtWidgets import QLabel, QPushButton, QWidget, QVBoxLayout, QFileDialog, QLineEdit, QHBoxLayout, QMessageBox
from PyQt5.QtGui import QIcon
from utils.tile_manger_tools import Tile_Tools
from PyQt5.QtCore import QThread,pyqtSignal

class Clear_Webp_Widget(QWidget):
    def __init__(self):
        super(QWidget, self).__init__()
        self.init_ui()

    def init_ui(self):
        self.resize(400, 250)
        self.setFixedSize(self.width(), self.height())
        self.setWindowTitle("清理webp空切片")
        self.setWindowIcon(QIcon("./assets/window_icon.png"))

        vbox = QVBoxLayout()
        input_line = QHBoxLayout()
        output_line = QHBoxLayout()
        button_line = QHBoxLayout()
        vbox.addLayout(input_line)
        vbox.addLayout(output_line)
        vbox.addLayout(button_line)

        self.setLayout(vbox)

        # 输入
        input_label = QLabel("输入：")
        self.input_edit_line = QLineEdit()
        input_dialog_botton = QPushButton("...")
        input_line.addWidget(input_label)
        input_line.addWidget(self.input_edit_line)
        input_line.addWidget(input_dialog_botton)
        input_dialog_botton.clicked.connect(self.click_input_button)

        # 输出
        output_label = QLabel("输出：")
        self.output_edit_line = QLineEdit()
        self.output_edit_line.setEnabled(False)
        output_dialog_botton = QPushButton("...")
        output_line.addWidget(output_label)
        output_line.addWidget(self.output_edit_line)
        output_line.addWidget(output_dialog_botton)
        output_dialog_botton.clicked.connect(self.click_output_button)
        # 提交按钮
        self.submit_button = QPushButton("提交处理")
        button_line.addWidget(self.submit_button)
        self.submit_button.clicked.connect(self.click_submit_button)

    def click_input_button(self):
        input_dir_path = QFileDialog.getExistingDirectory()
        if input_dir_path:
            self.input_edit_line.setText(input_dir_path)

    def click_output_button(self):
        output_dir_path = QFileDialog.getExistingDirectory()
        if output_dir_path:
            self.output_edit_line.setText(output_dir_path)

    def click_submit_button(self):
        if self.input_edit_line.text():
            self.submit_button.setEnabled(False)
            self.hide()
            self.thread = Complex_thread(self.input_edit_line.text())
            self.thread.start()
            self.thread.exit()
            self.thread.singnal.connect(self.tip)
        else:
            QMessageBox.warning(self, "警告", "请完善填写！", QMessageBox.Yes)
        
    def tip(self):
        QMessageBox.information(self,"信息", "完成处理！",QMessageBox.Yes)
        self.submit_button.setEnabled(True)

class Complex_thread(QThread):
    singnal = pyqtSignal()
    def __init__(self,input_edit_line):
        super().__init__()
        self.input_edit_line = input_edit_line
    
    def run(self):
        tool = Tile_Tools()
        tool.clear_function(self.input_edit_line)
        self.singnal.emit()
